package client_server;

import gdr.maps.MapPaths;

import java.rmi.*;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.Properties;
import java.util.Vector;

import javax.naming.InitialContext;

public class Server
{
	 Vector<String[]> playerList;
	 static Vector<AIplayer> nemici;
	 protected static Server instance;
	 protected static ServicesImpl impl;
	 protected static MapPaths map;
	 
	 
	  public static void main (String[] args){
		  Server s=new Server();
		  nemici=new Vector<AIplayer>();
		  instance = s;
		  s.run();
	  }
	 
	
	  private void creaNemici(boolean isEnemy){
		  AIplayer n = new AIplayer("nemico_"+nemici.size()+"", isEnemy);
		  n.start();
		  nemici.add(n);		  
		  String[] info = { n.nome_player,
		          n.stato,
		          n.orientation,
		          "" + n.location.x,
		          "" + n.location.y,
		          "",
		          ""
		 };
         try {
			impl.bornPlayer(info);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	  }
	  
	  
	  
	  
	  
	  
        public void run()
                {        	
        	    playerList = new Vector<String[]>();
        	    try
                   {
        	    	System.setProperty("java.security.policy","file:/Users/DeLac/eclipse_wrksp/hermes_android/GDR/wideopen.policy");
        	    	System.setProperty("java.rmi.server.codebase","file:/Users/DeLac/eclipse_wrksp/hermes_android/GDR/bin/");
        	    	System.out.println(System.getProperty("java.rmi.server.codebase"));
        	    	System.out.println(System.getProperty("java.security.policy")); 
        	    	//LocateRegistry.createRegistry(1099);
        	    	//Thread.sleep(2000);
        	    	if (System.getSecurityManager() == null)
            	    	System.setSecurityManager(new RMISecurityManager());
            	    	Properties env = new Properties();
                		Runtime.getRuntime().exec("rmiregistry");

                		Thread.sleep(2000);
                		//Runtime.getRuntime().exec("rmid -J-Djava.security.policy=rmid.policy");
                        impl = new ServicesImpl("myRMIImplInstance");
                        map = new MapPaths(impl.leggiOstacoli());
                        //creaNemici(true);
                        //creaNemici(false);
                        creaNemici(false);
                        System.out.println("server avviato");
                        }
                catch (Exception e)
                        {
                        System.out.println("Exception occurred: " + e);
                        e.printStackTrace();
                        }
                //try {UnicastRemoteObject.unexportObject(impl, true); } catch (NoSuchObjectException e) {} // shutdown
                }
        
        
        public  void addPlayer(String[] player){
  		  System.out.println(player[0]+" connesso");
        	playerList.add(player);
        }
        
        public  void removePlayer(String nomePlayer){
        	for(int i=0; i<playerList.size();i++)
        	 {
        	  if (playerList.get(i)[0].equals(nomePlayer))
        	    {
        		  playerList.remove(i);
        		  System.out.println(nomePlayer+" disconnesso");
        		  break;
        	    }
        	 }        	
        }
}
